<?php

/**
 * 心愿墙
 * */

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\Room;
use App\Http\Model\User;
use App\Http\Services\CommonSystemMsgServices;
use App\Http\Services\ContentCheckServices;
use App\Http\Services\MsgServices;
use App\Http\Services\RoomMsgServices;
use App\Http\Services\UserServices;
use Illuminate\Http\Request;


class ContentCheckController extends BaseController
{
    protected $contentCheckServices;
    protected $msgServices;
    protected $room_msg_services;
    protected $user_model;
    protected $room_model;

    public function __construct()
    {
        $this->contentCheckServices = new ContentCheckServices();
        $this->msgServices = new MsgServices();
        $this->room_msg_services = new RoomMsgServices();
        $this->user_model = new User();
        $this->room_model = new Room();
    }

    /**
     *  内容审核接口
     * */
    public function checkAudit()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }
            $this->callCommon($this->contentCheckServices, 'checkAudit', $this->requestData, false);

            //发送驳回消息
            if ($this->responseData['code'] == 0 && $this->responseData['data']) {
                $data = $this->responseData['data'];

                $checkObjArr = [
                    "1" => "用户昵称",
                    "2" => "用户签名",
                    "3" => "房间标题",
                    "4" => "房间公告",
                    "5" => "房间简介",
                    "6" => "我的声音",
                    "7" => "房间封面",
                    "8" => "相册/头像",
                    "9" => "心愿墙",
                    "10" => "发布动态",
                    "11" => "评论",
                    "12" => "评论",
                    "13" => "房间消息图片",
                    "14" => "公会名称",
                    "15" => "公会宣言",
                    "16" => "公会公告",
                    "17" => "公会马甲",
                    "18" => "家族名称",
                    "19" => "家族宣言",
                    "20" => "家族头像",
                    "21" => "叽喳讨论",
                    "22" => "叽喳评论",
                    "23" => "叽喳回复",
                    "24" => "作品",
                    "25" => "作品文本",
                    "26" => "折纸",
                ];

                foreach ($data as $key => $value) {
                    if ($this->requestData['status'] == 2) {
                        if(in_array($value['key'] ,array(14,15,16,17 ,18,19,20,21,22,23,24,25))){
                            $msg = '很抱歉，'.$checkObjArr[$value['key']].'未通过审核';
                        }
                        else{
                            if($value['key'] == 1){
                                $msg = "您上传的昵称未通过审核，请检查昵称内容是否合规，有疑问可联系客服人员~";
                            }else if($value['key'] == 8){
                                $msg = "您上传的头像未通过审核，请检查头像内容是否合规，有疑问可联系客服人员~";
                            }else if($value['key'] == 11 || $value['key'] == 12){
                                $msg = "您上传的评论未通过审核，请检查评论内容是否合规，有疑问可联系客服人员~";
                            }else if($value['key'] == 26) {//折纸
                                $msg = "您发布的折纸内容涉嫌违规，已自动撤回，请编辑后重新发布";
                            }else{
                                $msg = isset($checkObjArr[$value['key']]) ? $checkObjArr[$value['key']] : "内容";
                            }
                        }

                        if ($value['key'] == 26) {
                            (new CommonSystemMsgServices())->sendSystemMsgToUserV2($value['user_id'], 80051,
                                ['n' => '您发布的折纸内容涉嫌违规，已自动撤回，请编辑后重新发布'], 'kk:sys_text_msg');
                        } else {
                            $this->msgServices->send_sys_text_msg($value['user_id'], $msg);
                        }
                    }
                    //评论审核通过发消息
                    if ($this->requestData['status'] == 1 && $value['key'] == 11 && !empty($value['other_content_id'])) {
                        $con = json_decode($value['content'],true);
                        $this->msgServices->trends_comments($value['other_content_id'], $value['user_id'], $con['comment_text'], $con['comment_voice']);
                    }
                    //作品文本内容审核通过发消息
                    if ($this->requestData['status'] == 1 && $value['key'] == 25 ) {
                        $this->msgServices->send_sys_text_msg($value['user_id'], '你提交的自建内容已通过审核！快去使用自己的自建内容录制作品吧~');
                    }
                    if ($this->requestData['type'] == 1 || ($this->requestData['type'] == 2 && $this->requestData['status'] == 2)) {
                        if ($value['key'] == 3) {
                            $this->room_msg_services->broadcast_edit_name($value['data_id'], $value['content']);
                        }
                        if ($value['key'] == 4) {
                            $this->room_msg_services->broadcast_edit_notice($value['data_id'], $value['content']);
                        }
                        if ($value['key'] == 5) {
                            $this->room_msg_services->broadcast_edit_image($value['data_id'], $value['content']);
                        }
                    }


                    if ($value['key'] == 7) { //房间封面
                        //房间封面审核成功后，将头像写入房间信息中
                        if ($this->requestData['type'] == 1 && $this->requestData['status'] == 1) {
                            $this->room_model->set_image($value['data_id'], $value['content']);
                        } elseif ($this->requestData['type'] == 2 && $this->requestData['status'] == 2) {
                            //复审驳回,还原房间图片为默认图片
                            $image = "/icon/xiaowu1.png";
                            $this->room_model->set_image($value['data_id'], $image);

                        }
                    } elseif ($value['key'] == 8) { //头像
                        //头像初审通过，将头像写入用户信息中
                        if ($this->requestData['type'] == 1 && $this->requestData['status'] == 1) {
                            $user = $this->user_model->get_user($value['user_id']);
                            $user['images'][] = $value['content'];
                            $images = json_encode($user['images']);
                            $this->user_model->set_portrait($user['user_id'], $images);
                        } elseif ($this->requestData['type'] == 2 && $this->requestData['status'] == 2) {
                            //复审拒绝,删除用户头像
                            $user = $this->user_model->get_user($value['user_id']);
                            if (count($user['images']) > 0) {
                                foreach ($user['images'] as $k => $img) {
                                    if (basename($img) == basename($value['new_content'])) {
                                        unset($user['images'][$k]);
                                    }
                                }
                                $user['images'] = array_merge($user['images']);
                            }
                            $images = json_encode($user['images']);
                            $this->user_model->set_portrait($user['user_id'], $images);
                        }
                    }
                }
            }

        } while (false);

        $this->responseData();
    }


    /**
     * 循环处理图片检测
     */
    public function loopFilterImage()
    {
        $this->contentCheckServices->loopFilterImage();
    }
}
